﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace PrVyd.Zakladni_tridy
{
    class DatabaseManager
    {
        private static readonly string DB_PATH = "database.xml";
        private static readonly string DB_XSD_PATH = "db.xsd";

        public static void TryCreateDB()
        {
            if (File.Exists(DB_PATH))
                return;

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            using (XmlWriter writer = XmlWriter.Create(DB_PATH, settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("database");
                writer.WriteAttributeString("xmlns", "xsi", "http://www.w3.org/2000/xmlns/", "http://www.w3.org/2001/XMLSchema-instance");
                writer.WriteAttributeString("xsi", "noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "db.xsd");

                writer.WriteElementString("invoices", "");
                writer.WriteElementString("payments", "");

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
            
        }

        public static XDocument LoadDBX()
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.Schemas.Add("", DB_XSD_PATH);
            settings.ValidationType = ValidationType.Schema;

            XmlReader reader = XmlReader.Create(DB_PATH, settings);
            XDocument doc = new XDocument();
            try
            {
                doc = XDocument.Load(reader);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
                reader.Close();
                File.Delete(DB_PATH);
                TryCreateDB();

                reader = XmlReader.Create(DB_PATH, settings);
                doc = XDocument.Load(reader);
            }

            reader.Close();
            return doc;
        }

        public static XmlDocument LoadDBXml()
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.Schemas.Add("", DB_XSD_PATH);
            settings.ValidationType = ValidationType.Schema;

            XmlReader reader = XmlReader.Create(DB_PATH, settings);
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(reader);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e);
                reader.Close();
                File.Delete(DB_PATH);
                TryCreateDB();

                reader = XmlReader.Create(DB_PATH, settings);
                doc.Load(reader);
            }

            reader.Close();
            return doc;
        }

        public static void SaveDB(XDocument doc)
        {
            doc.Save(DB_PATH);
        }

        public static void SaveDB(XmlDocument doc)
        {
            if (doc == null)
                return;

            doc.Save(DB_PATH);
        }
    }
}
